package action.upload;

import java.io.File;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import staticclass.CommonConfig;

import com.opensymphony.xwork2.ActionSupport;

import common.impl.exception.ServiceException;
import common.interf.IErrorHandle;
import common.interf.IFileHandle;
import common.interf.IServletHandle;
/**
 * 上传视频文件
 * @author sky
 * 20150412
 */
public class UploadFileAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String fileFileName;// Struts2的拦截器FileUploadInterceptor根据请求对象中参数"fileInput"来自行生产的一个固有属性
	private File file;
	private IServletHandle servletHandle;
	private IErrorHandle errorHandle;
	private IFileHandle fileHandle;
	private String filePath="/upload/images";
	
	private String gameFileTmpPath="gamesTmp"; //游戏zip文件保存的临时文件夹
	private String gamesFilePath="games";
	
	
	
	
	public String getGamesFilePath() {
		return gamesFilePath;
	}

	public void setGamesFilePath(String gamesFilePath) {
		this.gamesFilePath = gamesFilePath;
	}

	public String getGameFileTmpPath() {
		return gameFileTmpPath;
	}

	public void setGameFileTmpPath(String gameFileTmpPath) {
		this.gameFileTmpPath = gameFileTmpPath;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
	
	public IServletHandle getServletHandle() {
		return servletHandle;
	}

	public void setServletHandle(IServletHandle servletHandle) {
		this.servletHandle = servletHandle;
	}

	public IErrorHandle getErrorHandle() {
		return errorHandle;
	}

	public void setErrorHandle(IErrorHandle errorHandle) {
		this.errorHandle = errorHandle;
	}

	
	public IFileHandle getFileHandle() {
		return fileHandle;
	}

	public void setFileHandle(IFileHandle fileHandle) {
		this.fileHandle = fileHandle;
	}

	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	/**
	 * 上传视频文件
	 * @return x:{"success":true,"result":"url"} url为文件在数据库中的存储地址
	 * @throws 
	 */
	public void uploadFile() throws Exception {
		String x="";
		try{
			x = fileHandle.saveUploadFile(file, ServletActionContext.getServletContext().getRealPath("/")+filePath, fileFileName);
			x = CommonConfig.domainName + ServletActionContext.getRequest().getContextPath() + filePath + "/" + x;
		}catch(Exception e){
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}
	
	/**
	 * 保存游戏zip文件
	 * @throws Exception
	 */
	public void uploadZipGameFile() throws Exception{
		//获取sessionId
		String sessionId = ServletActionContext.getRequest().getSession().getId();
		String r = "";
		try{
			//r = fileHandle.saveGameFile(file,ServletActionContext.getServletContext().getRealPath("/")+gameFileTmpPath,ServletActionContext.getServletContext().getRealPath("/")+gamesFilePath,fileFileName);
			//r = fileHandle.saveGameFile(file,ServletActionContext.getServletContext().getRealPath("/")+sessionId,ServletActionContext.getServletContext().getRealPath("/")+sessionId,fileFileName);
			//r = CommonConfig.domainName + ServletActionContext.getRequest().getContextPath() +"/"+sessionId+ "/" + r;
		}catch(Exception e){
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}
}